﻿Imports System.Data
Imports System.Data.SqlClient
Public Class Frm_Demande_Fournisseurs
    Public num As Integer = -1
    Public numf As Integer = -1
    Public modif As Integer = 0

    Public Sub vider()
        txt_fourni.Clear()
        txt_Quantité.Clear()
        txt_prix.Clear()
        txt_remarq.Clear()
        If Comb_Nom_Fournisseurs.Items.Count > 0 Then
            Comb_Nom_Fournisseurs.SelectedIndex = 0
        End If
        For Each i As ListViewItem In List_tous.Items
            i.Selected = False
        Next
    End Sub
    Public Sub affiche()
        Try
            Dim entr As String = ""
            List_tous.Items.Clear()
            Cmd = New SqlCommand("select * from Fournisseus, Fournisseurs_Demande where Fournisseus.Id_Fournisseur=Fournisseurs_Demande.Id_Fournisseur and id_demande_client=" & id_event, Con)

            open()
            Dr = Cmd.ExecuteReader()
            If Dr.HasRows = True Then
                Do While Dr.Read()
                    If Dr("Type_Fournisseur") = "Entreprise" Then
                        Dim Line As ListViewItem = New ListViewItem(New String() {Dr("Nom_entreprise"), Dr("Categorie"), Dr("id_demande")})
                        List_tous.Items.Add(Line)
                    Else
                        Dim Line As ListViewItem = New ListViewItem(New String() {Dr("Nom") & "  " & Dr("Prenom"), Dr("Categorie"), Dr("id_demande")})
                        List_tous.Items.Add(Line)

                    End If
                Loop
            End If
            Dr.Close()
            cnClose()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try
    End Sub

    Private Sub Frm_Demande_Fournisseurs_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '-------------------------------Remplir combo fornisseur---------------------------------'
        Try
            Comb_Nom_Fournisseurs.Items.Clear()
            Cmd = New SqlCommand("Select * From Fournisseus where type='Aliments'", Con)
            open()
            Dr = Cmd.ExecuteReader()
            Do While Dr.Read()
                If Dr("Type_Fournisseur") = "Entreprise" Then
                    Comb_Nom_Fournisseurs.Items.Add(Dr("Nom_entreprise"))
                Else
                    Comb_Nom_Fournisseurs.Items.Add(Dr("Nom") & "  " & Dr("Prenom"))
                End If
            Loop
            Dr.Close()
            cnClose()
            If Comb_Nom_Fournisseurs.Items.Count > 0 Then
                Comb_Nom_Fournisseurs.SelectedIndex = 0
            End If

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try
    End Sub
    Private Sub Comb_Nom_Fournisseurs_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_Nom_Fournisseurs.SelectedIndexChanged
        If Con.State = ConnectionState.Open Then
            cnClose()
        End If
        '--------------------------------------------------------------------------------------------------
        Try
            Cmd = New SqlCommand("Select * From Fournisseus", Con)
            open()
            Dr = Cmd.ExecuteReader()
            Do While Dr.Read()

                If Dr("Nom_entreprise") = Comb_Nom_Fournisseurs.SelectedItem.ToString Then
                    numf = Dr("Id_Fournisseur")
                ElseIf (Dr("Nom") & "  " & Dr("Prenom")) = Comb_Nom_Fournisseurs.SelectedItem.ToString Then
                    numf = Dr("Id_Fournisseur")
                End If
            Loop
            Dr.Close()
            cnClose()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try
        '--------------------------------------------------------------------------------------------------
        Try
            Comb_catégor.Items.Clear()
            Cmd = New SqlCommand("Select Categorie From Fournisseus Where Id_Fournisseur =" & numf, Con)
            open()
            Dr = Cmd.ExecuteReader()
            If Dr.HasRows Then
                Do While Dr.Read
                    Comb_catégor.Items.Add(Dr("Categorie"))
                Loop
            End If

            Dr.Close()
            cnClose()

            If Comb_catégor.Items.Count > 0 Then
                Comb_catégor.SelectedIndex = 0
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try
        For Each i As ListViewItem In List_tous.Items
            i.Selected = False
        Next
    End Sub
    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged
        Dim n As Integer
        With List_tous
            If .SelectedIndices.Count > 0 Then
                List_forni.Items.Clear()
                For i As Integer = 0 To .SelectedIndices.Count - 1
                    n = CInt(.Items(.SelectedIndices(i)).SubItems(2).Text)
                    modif = CInt(.Items(.SelectedIndices(i)).SubItems(2).Text)
                    Try
                        Cmd = New SqlCommand("Select * From fourniture Where id_demande=" & n, Con)
                        open()
                        Dr = Cmd.ExecuteReader()
                        If Dr.HasRows Then
                            Do While Dr.Read
                                Dim Line As ListViewItem = New ListViewItem(New String() {Dr("fourniture"), Dr("Quantite"), Dr("Prix"), Dr("Remarque"), Dr("Id")})
                                List_forni.Items.Add(Line)
                            Loop
                        End If
                        Dr.Close()
                        cnClose()

                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                    End Try

                Next
            Else
                List_forni.Items.Clear()
            End If
        End With

    End Sub

    Private Sub btn_rech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_rech.Click
        Frm_liste_Demande.MdiParent = Frm_Main
        Frm_liste_Demande.Show()
        liste = "demande fornisseur"
    End Sub

    Private Sub txt_client_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_client.TextChanged
        'If txt_client.Text <> "" Then
        '    List_tous.Items.Clear()
        '    List_forni.Items.Clear()
        '    affiche()
        'End If
    End Sub


    Private Sub btn_ajouFour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ajouFour.Click
        If txt_fourni.Text <> "" And txt_prix.Text <> "" And txt_Quantité.Text <> "" Then
            Dim Line As ListViewItem = New ListViewItem(New String() {txt_fourni.Text, txt_Quantité.Text, txt_prix.Text, txt_remarq.Text})
            List_forni.Items.Add(Line)
            txt_fourni.Clear()
            txt_Quantité.Clear()
            txt_prix.Clear()
            txt_prixTotl.Clear()
            txt_remarq.Clear()
        End If
    End Sub

    Private Sub btn_suppFour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_suppFour.Click
        For Each i As ListViewItem In List_forni.SelectedItems
            List_forni.Items.Remove(i)
        Next
        txt_fourni.Clear()
        txt_Quantité.Clear()
        txt_prix.Clear()
        txt_prixTotl.Clear()
        txt_remarq.Clear()
    End Sub

    Private Sub txt_prix_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_prix.KeyPress
        Dim allowedChars As String = "1234567890,"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

    Private Sub txt_prix_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_prix.TextChanged
        Try
            If txt_prix.Text <> "" And txt_Quantité.Text <> "" Then
                txt_prixTotl.Text = CDbl(txt_prix.Text) * CDbl(txt_Quantité.Text)
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try
    End Sub

    Private Sub txt_Quantité_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_Quantité.KeyPress
        Dim allowedChars As String = "1234567890,"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

    Private Sub txt_Quantité_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_Quantité.TextChanged
        Try
            If txt_prix.Text <> "" And txt_Quantité.Text <> "" Then
                txt_prixTotl.Text = CDbl(txt_prix.Text) * CDbl(txt_Quantité.Text)
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try
    End Sub

    Private Sub btn_ajouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ajouter.Click
        Dim idDem As Integer = -1
        Try
            Cmd = New SqlCommand("Select * From Fournisseus", Con)
            open()
            Dr = Cmd.ExecuteReader()
            Do While Dr.Read()

                If Dr("Nom_entreprise") = Comb_Nom_Fournisseurs.SelectedItem.ToString Then
                    num = Dr("Id_Fournisseur")
                ElseIf (Dr("Nom") & "  " & Dr("Prenom")) = Comb_Nom_Fournisseurs.SelectedItem.ToString Then
                    num = Dr("Id_Fournisseur")
                End If
            Loop
            Dr.Close()
            cnClose()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            cnClose()
        End Try
        '--------------------------------------------------------------------------------------------------------------------------------
        If txt_client.Text <> "" And List_forni.Items.Count > 0 Then
            If MsgBox("Vous Voulez Vraiment Ajouter ?", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Ajouter") = MsgBoxResult.Yes Then
                Try
                    Cmd = New SqlCommand("insert into Fournisseurs_Demande (Id_Fournisseur,id_demande_client)" & _
                    "values (@Id_Fournisseur,@id_demande_client)", Con)

                    Cmd.Parameters.Add("@Id_Fournisseur", SqlDbType.Int).Value = num
                    Cmd.Parameters.Add("@id_demande_client", SqlDbType.Int).Value = id_event

                    open()
                    Cmd.ExecuteNonQuery()
                    cnClose()
                    '---------------------------------------------------------------------------------------------
                    Try
                        Cmd = New SqlCommand("select id_demande from Fournisseurs_Demande", Con)
                        open()
                        Dr = Cmd.ExecuteReader()
                        Do While Dr.Read()
                            idDem = Dr("id_demande")
                        Loop
                        Dr.Close()
                        cnClose()
                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                        cnClose()
                    End Try
                    '---------------------------------------------------------------------------------------------
                    Try
                        For i As Integer = 0 To List_forni.Items.Count - 1
                            Cmd = New SqlCommand("insert into fourniture values (@id_demande,@fourniture,@Quantite,@Prix,@Remarque)", Con)
                            Cmd.Parameters.Add("@id_demande", SqlDbType.Int).Value = idDem
                            Cmd.Parameters.Add("@fourniture", SqlDbType.VarChar).Value = List_forni.Items(i).SubItems(0).Text
                            Cmd.Parameters.Add("@Quantite", SqlDbType.Float).Value = List_forni.Items(i).SubItems(1).Text
                            Cmd.Parameters.Add("@Prix", SqlDbType.Float).Value = List_forni.Items(i).SubItems(2).Text
                            Cmd.Parameters.Add("@Remarque", SqlDbType.VarChar).Value = List_forni.Items(i).SubItems(3).Text

                            open()
                            Cmd.ExecuteNonQuery()
                            cnClose()
                        Next
                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                        cnClose()
                    End Try
                    '--------------------------------------------------------------------------------------------
                    MsgBox("Ajouté Avec Succes", MsgBoxStyle.Information, "Réussi !")
                    vider()
                    affiche()
                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                    cnClose()
                End Try
            End If
        End If
    End Sub

    Private Sub btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_supp.Click
        Dim n As Integer

        If List_tous.SelectedIndices.Count > 0 And List_forni.SelectedIndices.Count < 1 Then
            With List_tous
                For i As Integer = 0 To .SelectedIndices.Count - 1
                    n = CInt(.Items(.SelectedIndices(i)).SubItems(2).Text)
                Next
                If MsgBox("Voulez Vous Vraiment Supprimer", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Supprission !") = MsgBoxResult.Yes Then
                    Try
                        Cmd = New SqlCommand("Delete From Fournisseurs_Demande Where id_demande =" & n, Con)
                        open()
                        Cmd.ExecuteNonQuery()
                        cnClose()
                        modif = 0
                        MsgBox("Suppresion effectué !", MsgBoxStyle.Information, "Terminer")
                        vider()
                        affiche()

                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                        cnClose()
                    End Try
                End If
            End With
        ElseIf List_forni.SelectedIndices.Count > 0 Then
            With List_forni
                For i As Integer = 0 To .SelectedIndices.Count - 1
                    n = CInt(.Items(.SelectedIndices(i)).SubItems(4).Text)
                Next
                If MsgBox("Voulez Vous Vraiment Supprimer", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Supprission !") = MsgBoxResult.Yes Then
                    Try
                        Cmd = New SqlCommand("Delete From fourniture Where Id =" & n, Con)
                        open()
                        Cmd.ExecuteNonQuery()
                        cnClose()
                        modif = 0
                        MsgBox("Suppresion effectué !", MsgBoxStyle.Information, "Terminer")
                        vider()
                        affiche()

                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                        cnClose()
                    End Try
                End If
            End With
        ElseIf List_tous.SelectedIndices.Count < 1 And List_forni.SelectedIndices.Count < 1 Then
            MsgBox("Vous n'avez selectionner aucun ligne !", MsgBoxStyle.Critical, "Réssayer !")

        End If

        '-------------------------------------------------------------------------------------------

    End Sub

    Private Sub btn_modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_modif.Click
        If modif <> 0 And List_tous.SelectedIndices.Count > 0 Then
            If MsgBox("Vous Voulez Vraiment Modifier ?", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "Modifier !") = MsgBoxResult.Yes Then
                '-----------------------------------------------------------------------------------
                Try
                    Cmd = New SqlCommand("Delete From fourniture Where id_demande =" & modif, Con)
                    open()
                    Cmd.ExecuteNonQuery()
                    cnClose()
                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                    cnClose()
                End Try
                '-----------------------------------------------------------------------------------
                Try
                    For i As Integer = 0 To List_forni.Items.Count - 1
                        Cmd = New SqlCommand("insert into fourniture values (@id_demande,@fourniture,@Quantite,@Prix,@Remarque)", Con)
                        Cmd.Parameters.Add("@id_demande", SqlDbType.Int).Value = modif
                        Cmd.Parameters.Add("@fourniture", SqlDbType.VarChar).Value = List_forni.Items(i).SubItems(0).Text
                        Cmd.Parameters.Add("@Quantite", SqlDbType.Float).Value = List_forni.Items(i).SubItems(1).Text
                        Cmd.Parameters.Add("@Prix", SqlDbType.Float).Value = List_forni.Items(i).SubItems(2).Text
                        Cmd.Parameters.Add("@Remarque", SqlDbType.VarChar).Value = List_forni.Items(i).SubItems(3).Text

                        open()
                        Cmd.ExecuteNonQuery()
                        cnClose()

                    Next
                    modif = 0
                    MsgBox("Modification a Réussi ", MsgBoxStyle.Information, "Réussi !")
                    vider()
                    affiche()
                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Exclamation)
                    cnClose()
                End Try
                '-----------------------------------------------------------------------------------

            End If
        End If

    End Sub


    Private Sub btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_nouv.Click
        vider()
    End Sub
End Class